<template>
  <div class="klinecharts-pro-select" @blur="open = false" tabIndex="0"
       :class="[open ? 'klinecharts-pro-select-show': '']">
    <div class="selector-container" @click="open = !open">
      <span class="value">{{value}}</span>
      <i class="arrow"/>
    </div>
    <div class="drop-down-container">
      <ul @click.prevent="open = false">
        <li v-for="item in dataSource" @click="$emit('change', item)">
          <span v-if="translate">{{$t(item[value_key])}}</span>
          <span v-else>{{item[value_key]}}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script setup lang="ts">
import {defineEmits, defineProps, ref} from "vue";

type PropType = {
  value?: string,
  value_key?: string,
  translate?: boolean,
  dataSource: any[]
}

const props = withDefaults(defineProps<PropType>(), {
  value_key: 'text',
  translate: false
})

const emit = defineEmits<{
  change: [value: any]
}>()

const open = ref(false);

</script>

<style scoped lang="scss">
@import '~/assets/klinebase.scss';

.#{$prefix-cls}-select {
  position: relative;
  display: flex;
  flex-direction: row;
  align-items: center;
  cursor: pointer;
  height: 34px;
  width: 100px;
  outline: none;
  color: var(--klinecharts-pro-text-color);
  font-size: 14px;
  .selector-container {
    display: flex;
    flex-direction: row;
    align-items: center;
    height: 100%;
    width: 100%;
    padding: 0 10px;
    justify-content: space-between;
    border: solid 1px var(--klinecharts-pro-border-color);
    border-radius: 2px;
    box-sizing: border-box;
    .arrow {
      display: inline;
      width: 0;
      height: 0;
      border-left: 5px solid transparent;
      border-right: 5px solid transparent;
      border-top: 6px solid var(--klinecharts-pro-text-color);
      transition: all .2s;
    }
  }
  .drop-down-container {
    background-color: var(--klinecharts-pro-popover-background-color);
    box-shadow: 0 6px 12px 0 rgba(0, 0, 0, .3);
    position: absolute;
    left: 0;
    width: 100%;
    top: calc(100% + 1px);
    transition: all .2s;
    transform: scaleY(0);
    transform-origin: top;
    opacity: 0;
    z-index: 100;
    border-radius: 2px;
    max-height: 200px;
    overflow-y: auto;
    ul {
      list-style: none;
      margin: 0;
      padding: 0;
      overflow: hidden;
      li {
        display: flex;
        flex-direction: row;
        align-items: center;
        height: 40px;
        padding: 0 10px;
        &:first-child {
          border-top-left-radius: 2px;
          border-top-right-radius: 2px;
        }
        &:last-child {
          border-bottom-left-radius: 2px;
          border-bottom-right-radius: 2px;
        }
        &:hover {
          background-color: var(--klinecharts-pro-hover-background-color);
        }
      }
    }
  }
}

.#{$prefix-cls}-select-show {
  .selector-container {
    border-color: var(--klinecharts-pro-primary-color)!important;
    .value {
      color: var(--klinecharts-pro-primary-color);
    }
    .arrow {
      transform: rotate(180deg);
    }
  }
  .drop-down-container {
    transform: scaleY(1)!important;
    opacity: 1!important;
  }
}

</style>